var Splinner = function(image, arr, x, y, context) {
    this.arr = arr;
    this.image = image;
    this.buffer = CanvasUtils.createBuffer(image.width, image.height);
    this.rotate = 0;
    this._direcion = null;
    this.rnd1 =RandomUlils.getRandomInt(-50,50);
    this.rnd2 =RandomUlils.getRandomInt(50,100);

    this.x = x;
    this.y = y;
    var pattern = context.createPattern(this.image, 'repeat');
    this.buffer.fillStyle = pattern;
    this.buffer.lineWidth = 0;
    this.buffer.beginPath();
    for(var i=0; i<this.arr.length; i+=2) {
        if(i==0) {
            this.buffer.moveTo(this.arr[i], this.arr[i+1]);
            if(this.arr[i] == 0) {
                this._direcion = "left";
            }
            else if(this.arr[i] == this.image.width) {
                this._direcion = "right";
            }
            else if(this.arr[i+1] == 0) {
                this._direcion = "top";
            }
            else if(this.arr[i+1] == this.image.height) {
                this._direcion = "bottom";
            }
        }
        else {
            this.buffer.lineTo(this.arr[i], this.arr[i+1]);
        }
    };

    this.buffer.stroke();
    this.buffer.shadowColor = '#999';
    this.buffer.shadowBlur = 10;
    this.buffer.shadowOffsetX = 5;
    this.buffer.shadowOffsetY = 5;
    this.buffer.fill();
    this.buffer.closePath();
};

Splinner.createSplinnersByPoint = function(img, x, y, x1, y1, context) {

    if(img.width>1000 || img.height>1000) {
        return [];
    }
    var splinners = [];
    var prevX = 0;

    var stepW = img.width/RandomUlils.getRandomInt(3, 6);
    stepW = stepW<20 ? 20 : stepW;
    var stepH = img.height/RandomUlils.getRandomInt(3, 6);
    stepH = stepH<20 ? 20 : stepH;

    for(var i =stepW; i<=img.width; i+=stepW) {
        splinners.push(new Splinner(img, [i, 0, x, y, prevX, 0], x1, y1,context));
        splinners.push(new Splinner(img, [i, img.height, x, y, prevX, img.height], x1, y1,context));
        prevX = i;
    }
    var prevY = 0;
    for(var i =stepH; i<=img.height; i+=stepH) {
        splinners.push(new Splinner(img, [0, i, x, y, 0, prevY], x1, y1,context));
        splinners.push(new Splinner(img, [img.width, i, x, y, img.width, prevY], x1, y1,context));
        prevY = i;
    }
    return splinners;
};


Splinner.prototype.getDirection = function() {
    return this._direcion;
}

Splinner.prototype.draw = function(context) {
    context.save()

    switch(this._direcion) {
        case "top":
            this.y-=this.rnd2;
            this.x-=this.rnd1;
            break;
        case "bottom":
            this.y+=this.rnd2+50;
            this.x-=this.rnd1;
            break;
        case "left":
            this.x-=this.rnd2;
            this.y+=this.rnd1+50;
            break;
        case "right":
            this.x+=this.rnd2;
            this.y+=this.rnd1+50;
            break;
    }
    //context.translate(getRandomInt(0, 700), getRandomInt(0, 700));
    //context.rotate(this.rotate+=RandomUlils.getRandomArbitary(-0.01,0.01));
    context.drawImage(this.buffer.canvas, this.x, this.y);
    context.restore();
};